package com.hanxing.kxcharge.module.operations.service.costtemplateprice;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.hanxing.kxcharge.module.operations.controller.admin.costtemplateprice.vo.*;
import com.hanxing.kxcharge.module.operations.dal.dataobject.costtemplateprice.CostTemplatePriceDO;
import com.hanxing.kxcharge.framework.common.pojo.PageResult;
import com.hanxing.kxcharge.framework.common.pojo.PageParam;
import com.hanxing.kxcharge.framework.common.util.object.BeanUtils;

import com.hanxing.kxcharge.module.operations.dal.mysql.costtemplateprice.CostTemplatePriceMapper;

import static com.hanxing.kxcharge.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.hanxing.kxcharge.module.operations.enums.ErrorCodeConstants.*;

/**
 * 计费模板价格时段 Service 实现类
 *
 * @author 航兴充电
 */
@Service
@Validated
public class CostTemplatePriceServiceImpl implements CostTemplatePriceService {

    @Resource
    private CostTemplatePriceMapper costTemplatePriceMapper;

    @Override
    public Long createCostTemplatePrice(CostTemplatePriceSaveReqVO createReqVO) {
        // 插入
        CostTemplatePriceDO costTemplatePrice = BeanUtils.toBean(createReqVO, CostTemplatePriceDO.class);
        costTemplatePriceMapper.insert(costTemplatePrice);
        // 返回
        return costTemplatePrice.getId();
    }

    @Override
    public void updateCostTemplatePrice(CostTemplatePriceSaveReqVO updateReqVO) {
        // 校验存在
        validateCostTemplatePriceExists(updateReqVO.getId());
        // 更新
        CostTemplatePriceDO updateObj = BeanUtils.toBean(updateReqVO, CostTemplatePriceDO.class);
        costTemplatePriceMapper.updateById(updateObj);
    }

    @Override
    public void deleteCostTemplatePrice(Long id) {
        // 校验存在
        validateCostTemplatePriceExists(id);
        // 删除
        costTemplatePriceMapper.deleteById(id);
    }

    private void validateCostTemplatePriceExists(Long id) {
        if (costTemplatePriceMapper.selectById(id) == null) {
            throw exception(COST_TEMPLATE_PRICE_NOT_EXISTS);
        }
    }

    @Override
    public CostTemplatePriceDO getCostTemplatePrice(Long id) {
        return costTemplatePriceMapper.selectById(id);
    }

    @Override
    public PageResult<CostTemplatePriceDO> getCostTemplatePricePage(CostTemplatePricePageReqVO pageReqVO) {
        return costTemplatePriceMapper.selectPage(pageReqVO);
    }

}